package com.xunbao.thread.util;


import com.xunbao.utils.DateUtils;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * ClassName:ThreadPoolExecutorTest
 * Description:
 * Author:yijiawei
 * Date: 2016/5/30 9:13
 */
public class ThreadPoolExecutorTest {

    public static void main(String[] args) {

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
//        System.out.println(DateUtils.getNowDateStr());
//        scheduledExecutorService.schedule(new MyTask("one"), 5, TimeUnit.SECONDS);
//        scheduledExecutorService.schedule(new MyTask("two"), 5, TimeUnit.SECONDS);
//        scheduledExecutorService.schedule(new MyTask("three"), 6, TimeUnit.SECONDS);
//        System.out.println(DateUtils.getNowDateStr());


        List<Future<ResultObject>> futureList = new ArrayList<Future<ResultObject>>();
        System.out.println(DateUtils.getNowDateStr());
        Future<ResultObject> future = scheduledExecutorService.schedule(new MyTaskWithResult("one"), 5, TimeUnit.SECONDS);
        futureList.add(future);
        future = scheduledExecutorService.schedule(new MyTaskWithResult("two"), 15, TimeUnit.SECONDS);
        futureList.add(future);
        System.out.println("over");
        System.out.println(futureList.size());
        for (int i = 0; i < futureList.size(); i++) {
            System.out.println(DateUtils.getNowDateStr());

            boolean isDone = futureList.get(i).isDone();
            System.out.println(isDone);
            try {
                System.out.println("ResultObject = " + JSONObject.fromObject(futureList.get(i).get()));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            isDone = futureList.get(i).isDone();
            System.out.println(isDone);
            boolean isCancel = futureList.get(i).isCancelled();
            System.out.println("isCancel = " + isCancel);
            System.out.println(DateUtils.getNowDateStr());
        }


        BuildFutureTask buildFutureTask = new BuildFutureTask(new MyTaskWithResult("one"));
        scheduledExecutorService.submit(buildFutureTask);

    }
}
